package com.accessibleworlds.report;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.accessibleworlds.R;
import com.accessibleworlds.user.Options;
import com.accessibleworlds.user.UserPage;
import com.accessibleworlds.util.Connections;
import com.accessibleworlds.util.SharedVariables;

public class CreateViolationReport extends Activity implements OnClickListener{
	
	private String latitude,longitude;
	
	private TextView title,
		titleTitle,titleNecessary,titleProblem,
		descriptionTitle,descriptionNecessary,descriptionProblem,
		typeTitle,typeNecessary,typeProblem,
		statusTitle,statusNecessary,statusProblem;
	
	private EditText titleEdit,descriptionEdit;
	
	private Spinner typeSpinner;
	
	private RadioButton normalRadio,minorRadio,urgentRadio,fixedRadio;
	
	private Button submit,cancel;
	
	private boolean isVisible;
	
	private ProgressDialog progressDialog;
	
	private SharedPreferences preferences;
	
	private Dialog alertDialog;
	
	private String reportID;
	
	private int imageCapture = 1;
	private int videoCapture = 2;
	private File imageFile;
	private File videoFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)	{
		    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		}
		
		setContentView(R.layout.createviolationreport);
		
		isVisible = true;
		
		Bundle bundle = getIntent().getExtras();
		latitude = bundle.getString("latitude");
		longitude = bundle.getString("longitude");
		
		preferences = getSharedPreferences(SharedVariables.fileName, MODE_PRIVATE);
        
		initializeVariables();
        changeLanguage();
        
        new GetViolationTypesTask().execute();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		isVisible=true;
        changeLanguage();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		isVisible = false;
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this,UserPage.class);
		Bundle bundle = new Bundle();
		bundle.putString("previousID", SharedVariables.searchReportID);
		intent.putExtras(bundle);
		if(isVisible) {
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
		finish();
	}
	
	private void initializeVariables() {
		title = (TextView)findViewById(R.id.createViolationReport);
		titleTitle = (TextView)findViewById(R.id.createViolationReportTitleTitle);
		titleNecessary = (TextView)findViewById(R.id.createViolationReportTitleNecessary);
		titleProblem = (TextView)findViewById(R.id.createViolationReportTitleProblem);
		descriptionTitle = (TextView)findViewById(R.id.createViolationReportDescriptionTitle);
		descriptionNecessary = (TextView)findViewById(R.id.createViolationReportDescriptionNecessary);
		descriptionProblem = (TextView)findViewById(R.id.createViolationReportDescriptionProblem);
		typeTitle = (TextView)findViewById(R.id.createViolationReportTypeTitle);
		typeNecessary = (TextView)findViewById(R.id.createViolationReportTypeNecessary);
		typeProblem = (TextView)findViewById(R.id.createViolationReportTypeProblem);
		statusTitle = (TextView)findViewById(R.id.createViolationReportStatusTitle);
		statusNecessary = (TextView)findViewById(R.id.createViolationReportStatusNecessary);
		statusProblem = (TextView)findViewById(R.id.createViolationReportStatusProblem);
		
		titleEdit = (EditText)findViewById(R.id.createViolationReportTitleEdit);
		descriptionEdit = (EditText)findViewById(R.id.createViolationReportDescriptionEdit);		
		
		typeSpinner = (Spinner)findViewById(R.id.createViolationReportTypeSpinner);
		
		normalRadio = (RadioButton)findViewById(R.id.createViolationReportStatusDetectedRadio);
		minorRadio = (RadioButton)findViewById(R.id.createViolationReportStatusStillSameRadio);
		urgentRadio = (RadioButton)findViewById(R.id.createViolationReportStatusInProgressRadio);
		fixedRadio = (RadioButton)findViewById(R.id.createViolationReportStatusFixedRadio);
		
		submit = (Button)findViewById(R.id.createViolationReportSubmit);
		cancel = (Button)findViewById(R.id.createViolationReportCancel);
		
		submit.setOnClickListener(this);
		cancel.setOnClickListener(this);
		
		progressDialog = new ProgressDialog(CreateViolationReport.this);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
	}
	
	private void changeLanguage() {
		title.setText(getResources().getStringArray(R.array.createViolationReportUpper)[SharedVariables.currentLanguage]);
		
		titleTitle.setText(getResources().getStringArray(R.array.title)[SharedVariables.currentLanguage]);
		titleProblem.setText(getResources().getStringArray(R.array.titleProblem)[SharedVariables.currentLanguage]);		
		descriptionTitle.setText(getResources().getStringArray(R.array.description)[SharedVariables.currentLanguage]);
		descriptionProblem.setText(getResources().getStringArray(R.array.descriptionProblem)[SharedVariables.currentLanguage]);
		typeTitle.setText(getResources().getStringArray(R.array.violationType)[SharedVariables.currentLanguage]);
		typeProblem.setText(getResources().getStringArray(R.array.typeProblem)[SharedVariables.currentLanguage]);
		statusTitle.setText(getResources().getStringArray(R.array.violationStatus)[SharedVariables.currentLanguage]);
		statusProblem.setText(getResources().getStringArray(R.array.statusProblem)[SharedVariables.currentLanguage]);
		
		normalRadio.setText(getResources().getStringArray(R.array.normal)[SharedVariables.currentLanguage]);
		minorRadio.setText(getResources().getStringArray(R.array.minor)[SharedVariables.currentLanguage]);
		urgentRadio.setText(getResources().getStringArray(R.array.urgent)[SharedVariables.currentLanguage]);
		fixedRadio.setText(getResources().getStringArray(R.array.fixed)[SharedVariables.currentLanguage]);
		
		submit.setText(getResources().getStringArray(R.array.submit)[SharedVariables.currentLanguage]);
		cancel.setText(getResources().getStringArray(R.array.cancel)[SharedVariables.currentLanguage]);
		
		progressDialog.setMessage(getResources().getStringArray(R.array.loadingMessage)[SharedVariables.currentLanguage]);
	}
	
	public void onClick(View v) {
		
		if(v.getId()==submit.getId()){
			InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(titleEdit.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(descriptionEdit.getWindowToken(), 0);
			
			String titleString = titleEdit.getText().toString();
			String descriptionString = descriptionEdit.getText().toString();
			
			String typeString = "";
			if(typeSpinner.getSelectedItemPosition() != 0) {
				typeString = ""+typeSpinner.getSelectedItemPosition();
			}
			
			String statusString = "";
			if(normalRadio.isChecked()) {
				statusString = "normal";
			}
			else if(minorRadio.isChecked()) {
				statusString = "minor";
			}
			else if(urgentRadio.isChecked()) {
				statusString = "urgent";
			}
			else if(fixedRadio.isChecked()) {
				statusString = "fixed";
			}
			
			new CreateViolationReportTask().execute(new String [] {titleString,descriptionString,typeString,statusString});
		}
		else if(v.getId()==cancel.getId()){
			Intent intent = new Intent(this,UserPage.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.searchReportID);
			intent.putExtras(bundle);
			if(isVisible) {
				startActivity(intent);
				overridePendingTransition(R.anim.fade, R.anim.hold);
			}
			finish();
		}
	}
	
	private boolean checkVariablesBackground(String titleString,String descriptionString,String typeString,String statusString) {
		
		boolean check = true;
				
		if(!(titleString.length()>0 && titleString.length()<=45)) {
			check = false;
		}
		
		if(!(descriptionString.length()>0 && descriptionString.length()<=100)) {
			check = false;
		}
		
		if(typeString.length()==0) {
			check=false;
		}
		
		if(statusString.length()==0) {
			check=false;
		}
		
		return check;
	}
	
	private void checkVariables(String titleString,String descriptionString,String typeString,String statusString) {
		
		if(!(titleString.length()>0 && titleString.length()<=45)) {
			titleNecessary.setTextColor(getResources().getColor(R.color.red));
			titleProblem.setTextColor(getResources().getColor(R.color.red));
		}
		
		if(!(descriptionString.length()>0 && descriptionString.length()<=100)) {
			descriptionNecessary.setTextColor(getResources().getColor(R.color.red));
			descriptionProblem.setTextColor(getResources().getColor(R.color.red));
		}
		
		if(typeString.length()==0) {
			typeNecessary.setTextColor(getResources().getColor(R.color.red));
			typeProblem.setTextColor(getResources().getColor(R.color.red));
		}
		
		if(statusString.length()==0) {
			statusNecessary.setTextColor(getResources().getColor(R.color.red));
			statusProblem.setTextColor(getResources().getColor(R.color.red));
		}
	}
	
	private void makeDefautColor() {
		titleNecessary.setTextColor(getResources().getColor(R.color.backgroundColor));
		titleProblem.setTextColor(getResources().getColor(R.color.backgroundColor));
		descriptionNecessary.setTextColor(getResources().getColor(R.color.backgroundColor));
		descriptionProblem.setTextColor(getResources().getColor(R.color.backgroundColor));
		typeNecessary.setTextColor(getResources().getColor(R.color.backgroundColor));
		typeProblem.setTextColor(getResources().getColor(R.color.backgroundColor));
		statusNecessary.setTextColor(getResources().getColor(R.color.backgroundColor));
		statusProblem.setTextColor(getResources().getColor(R.color.backgroundColor));
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK) {
			if(requestCode == imageCapture) {
				try {
					Bundle bundle = data.getExtras();
					Bitmap bitmap = (Bitmap) bundle.get("data");
					
					File folder = new File(SharedVariables.folderName);
					if(!folder.exists()) {
						folder.mkdirs();
					}
					
					ByteArrayOutputStream bytes = new ByteArrayOutputStream();
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
					
					File file = new File(SharedVariables.folderName+File.separator+"Temp.jpeg");
					file.createNewFile();
					
					FileOutputStream output = new FileOutputStream(file);
					output.write(bytes.toByteArray());
					output.flush();
					output.close();
					
					imageFile = new File(SharedVariables.folderName+File.separator+"Temp.jpeg");
					
					new ImageTask().execute("");
					
				} catch (Exception e) {
					
				}
			}
			else if(requestCode == videoCapture) {
				try {
					
					videoFile = new File(getRealPathFromURI(data.getData()));
					
					new VideoTask().execute("");
					
				} catch (Exception e) {
					
				}
			}
		}
		else {
			mediaAlertDialog();
		}
	};
	
	public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
	
	public class GetViolationTypesTask extends AsyncTask<Void, Integer, String>{

		private String[] takenData = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			
			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.getViolationTypes;
					String [] parameters = {"accessCode"};
					String [] paramaterValues = {SharedVariables.accessCode};
					
					takenData = Connections.connection(url,parameters,paramaterValues);
					
					if("OK".equalsIgnoreCase(takenData[0])) {
						publishProgress(1);
						return "OK";
					}
					else {
						publishProgress(0);
					}
				}
				catch (Exception ex) {
					publishProgress(0);
				}
			}
			else {
				publishProgress(0);
			}
			return "Fail";
		}
		
		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(CreateViolationReport.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetViolationTypesTask().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				takenData[0] = "Select Violation Type...";
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, takenData);
				typeSpinner.setAdapter(adapter);
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				Toast.makeText(CreateViolationReport.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}
		
		@Override
		protected void onPostExecute(String result) {}
	}

	public class CreateViolationReportTask extends AsyncTask<String, Integer, String> {
		
		private String titleString;
		private String descriptionString;
		private String typeString;
		private String statusString;

		private boolean check = true;
		private String [] takenData = null;
		
		protected void onPreExecute() {
			progressDialog.show();
			makeDefautColor();
		}
	
		@Override
		protected String doInBackground(String... params) {
			titleString = params[0];
			descriptionString = params[1];
			typeString = params[2];
			statusString = params[3];
			
			if(Connections.serviceStatus()) {
				check = checkVariablesBackground(titleString, descriptionString, typeString, statusString);
				if(check) {
					try {
						String url = Connections.URI+Connections.createReport;
						String [] parameters = {"accessCode","userID","title","description","latitude","longitude","type","status"};
						
						String [] paramaterValues = {SharedVariables.accessCode,preferences.getString("userID", ""),titleString,descriptionString,latitude,longitude,typeString,statusString};
						
						takenData = Connections.connection(url,parameters,paramaterValues);
						if("OK".equalsIgnoreCase(takenData[0])) {
							reportID = takenData[1];
							publishProgress(2);
							return "OK";
						}
						else {
							publishProgress(3);
						}
					} catch (Exception e) {
						publishProgress(3);
					}
				}
				else {
					publishProgress(1);
				}
			}
			else {
				publishProgress(0);
			}
			return "Fail";
		}
	
		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(CreateViolationReport.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new CreateViolationReportTask().execute(new String [] {titleString,descriptionString,typeString,statusString});
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				checkVariables(titleString, descriptionString, typeString, statusString);
				Toast.makeText(CreateViolationReport.this, getResources().getStringArray(R.array.parameterProblemToast)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				mediaAlertDialog();
			}
			else if(progress[0]==3) {
				progressDialog.dismiss();
				Toast.makeText(CreateViolationReport.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}
	
		protected void onPostExecute(String result) {}
	}
	
	public class ImageTask extends AsyncTask<String, Integer, String> {
		
		private String [] takenData = null;
		
		protected void onPreExecute() {
			progressDialog.show();
		}
	
		@Override
		protected String doInBackground(String... params) {
			if(Connections.serviceStatus()) {
				try {						
					String url = Connections.URI+Connections.uploadImage;
					String [] parameters = {"accessCode","reportID","userID"};
					String [] paramaterValues = {SharedVariables.accessCode,reportID,preferences.getString("userID", "")};
					
					takenData = Connections.uploadMedia(url,parameters,paramaterValues,imageFile);
					if("OK".equalsIgnoreCase(takenData[0])) {
						publishProgress(1);
						return "OK";
					}
					else {
						publishProgress(2);
					}
				} catch (Exception e) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return "Fail";
		}
	
		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(CreateViolationReport.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new ImageTask().execute("");
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				mediaAlertDialog();
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				Toast.makeText(CreateViolationReport.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}
	
		protected void onPostExecute(String result) {}
	}
	
	public class VideoTask extends AsyncTask<String, Integer, String> {
		
		private String [] takenData = null;
		
		protected void onPreExecute() {
			progressDialog.show();
		}
	
		@Override
		protected String doInBackground(String... params) {
			if(Connections.serviceStatus()) {
				try {						
					String url = Connections.URI+Connections.uploadVideo;
					String [] parameters = {"accessCode","reportID","userID"};
					String [] paramaterValues = {SharedVariables.accessCode,reportID,preferences.getString("userID", "")};
					
					takenData = Connections.uploadMedia(url,parameters,paramaterValues,videoFile);
					if("OK".equalsIgnoreCase(takenData[0])) {
						publishProgress(1);
						return "OK";
					}
					else {
						publishProgress(2);
					}
				} catch (Exception e) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return "Fail";
		}
	
		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(CreateViolationReport.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new VideoTask().execute("");
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				mediaAlertDialog();
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				Toast.makeText(CreateViolationReport.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}
	
		protected void onPostExecute(String result) {}
	}
	
	private void mediaAlertDialog() {
		alertDialog = new Dialog(CreateViolationReport.this);
		alertDialog.setContentView(R.layout.pop_up_with_three_button);
		alertDialog.setTitle("Add Media File");
		Button addImage = (Button) alertDialog.findViewById(R.id.threebuttons_choice1);
		addImage.setText("Add Image");
		Button addVideo = (Button) alertDialog.findViewById(R.id.threebuttons_choice2);
		addVideo.setText("Add Video");
		Button addNothing = (Button) alertDialog.findViewById(R.id.threebuttons_choice3);
		addNothing.setText("Add Nothing");
		alertDialog.show();
		addImage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				if(isVisible) {
					alertDialog.dismiss();
					startActivityForResult(intent, imageCapture);
					overridePendingTransition(R.anim.fade, R.anim.hold);
				}
			}
		});
		addVideo.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
				if(isVisible) {
					alertDialog.dismiss();
					startActivityForResult(intent, videoCapture);
					overridePendingTransition(R.anim.fade, R.anim.hold);
				}
			}
		});
		addNothing.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(CreateViolationReport.this,UserPage.class);
				Bundle bundle = new Bundle();
				bundle.putString("previousID", SharedVariables.searchReportID);
				intent.putExtras(bundle);
				if(isVisible) {
					startActivity(intent);
					overridePendingTransition(R.anim.fade, R.anim.hold);
				}
				alertDialog.dismiss();
				finish();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mainMenu = getMenuInflater();
		mainMenu.inflate(R.menu.mainmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem((R.id.menuOptions)).setTitle(getResources().getStringArray(R.array.options)[SharedVariables.currentLanguage]);
		menu.findItem(R.id.menuOptions).setIcon(R.drawable.optionsicon);
		menu.findItem((R.id.menuExit)).setTitle(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage]);
		menu.findItem((R.id.menuExit)).setIcon(R.drawable.exiticon);
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==R.id.menuOptions) {
			Intent intent = new Intent(this,Options.class);
			Bundle bundle = new Bundle();
			bundle.putString("previousID", SharedVariables.creatViolationReportID);
			intent.putExtras(bundle);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
		else if (item.getItemId()==R.id.menuExit) {
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
}
